//
//  AddressViewController.swift
//  二次源
//
//  Created by 李建文 on 2016/11/22.
//  Copyright © 2016年 二次源. All rights reserved.
//

import UIKit

class AddressViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    //返回
    @IBOutlet weak var Back: UIButton!
    
    //地址列表
    @IBOutlet weak var AddressTable: UITableView!
    
    var addressInfo:[String] = []
    var phoneInfo:[String] = []
    var adnameInfo:[String] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        //协议、代理
        self.AddressTable.delegate = self
        self.AddressTable.dataSource = self
        self.AddressTable!.register(UINib(nibName:"AdressTableViewCell", bundle:nil),
                                 forCellReuseIdentifier:"addressCell")
        let net:Net = Net()
        net.addUrl(url: "/querryAddress?uid=1")
        //self.net.addParames(key: "uid", value: "2")
        net.sendPostWithParam()
        //延时1秒执行
        let time: TimeInterval = 1.0
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
            let data:JSON = net.getData()
//            print(data)
            let a = data[0]["count"][0]["count(address)"].description
            let b:Int = Int(a)!
            print(a)
            for i:Int in 0 ..< b {
                let message = data[0]["address"]
                let address = message[i]["address"]
                let message2 = data[0]["adphone"]
                let adphone = message2[i]["adphone"]
                let message3 = data[0]["adname"]
                let adname = message3[i]["adname"]
                self.addressInfo.append(address.description)
                self.phoneInfo.append(adphone.description)
                self.adnameInfo.append(adname.description)
                self.AddressTable.reloadData()
            }
            print(self.addressInfo)
        }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @IBAction func didBack(_ sender: AnyObject) {
        //self.performSegue(withIdentifier: "toMy", sender: nil)
        self.dismiss(animated: true, completion:nil) 
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return self.addressInfo.count
        return addressInfo.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:AdressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "addressCell") as! AdressTableViewCell
        cell.address.text = addressInfo[indexPath.row]
        cell.phone.text = phoneInfo[indexPath.row]
        cell.name.text = adnameInfo[indexPath.row]
        return cell
    }
    
    //单元格高度
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath)
        -> CGFloat {
            return 130
    }

}
